/**
 * Created by changchengQin on 2016-5-10.
 */

/*ag-grid 下拉选择框 编辑器*/
// function to act as a class
function RcSelectCellEditor() {
}

RcSelectCellEditor.prototype.init = function (params) {
    this.selectDiv = document.createElement('div');
    this.selectDiv.className = "ag-cell-edit-input";
    this.eSelect = document.createElement('select');
    this.eSelect.className = "ag-cell-edit-input";
    this.selectDiv.appendChild(this.eSelect);

    if (!params.values) {
        return;
    }
    for(code in params.values){
        var option = document.createElement('option');
        option.value = code;
        option.text = params.values[code];
        if (params.value === code) {
            option.selected = true;
        }
        this.eSelect.appendChild(option);
    }
    this.eSelect.addEventListener('change', function () { return params.stopEditing(); });
};

RcSelectCellEditor.prototype.afterGuiAttached = function () {
    this.eSelect.focus();
};

// gets called once when grid ready to insert the element
RcSelectCellEditor.prototype.getGui = function () {
    return this.selectDiv;
};

RcSelectCellEditor.prototype.getValue = function () {
    return this.eSelect.value;
};


//大文本（备注）编辑器
function LargeTextCellEditor() {
}

LargeTextCellEditor.prototype.init = function (params) {
    this.textarea = document.createElement("textarea");
    this.textarea.maxLength = "200";
    this.textarea.cols = "60";
    this.textarea.rows = "10";
    this.textarea.value = params.value;

    this.eInput = document.createElement("div");
    this.eInput.appendChild(this.textarea);

    // allow arrow keys and enter in the textarea
    // tab, esc etc will still end the editing
    this.textarea.addEventListener('keydown', function (event) {
        var charCode = getCharCodeFromEvent(event);
        if(charCode == 37 ||            // left
            charCode == 38 ||       // up
            charCode == 39 ||       // right
            charCode == 40 ||       // down
            charCode == 13) {       // enter
            event.stopPropagation();
        }
    })
};

// gets called once when grid ready to insert the element
LargeTextCellEditor.prototype.getGui = function () {
    return this.eInput;
};

LargeTextCellEditor.prototype.afterGuiAttached = function () {
    this.textarea.focus();
};

LargeTextCellEditor.prototype.getValue = function () {
    return this.textarea.value;
};

// any cleanup we need to be done here
LargeTextCellEditor.prototype.destroy = function () {
};

LargeTextCellEditor.prototype.isPopup = function () {
    return true;
};

